// Test of TMP37 and LDR sensors.
// 12-04-2010 <mdoll@usf.edu> http://opensource.org/licenses/mit-license.php

#include <Ports.h>
#include <RF12.h>
#include <HomeNet.h>
#include <HomeNetDevices.h>

//Start HomeNet Packet Stack
HomeNet stack(0x02);//0x01 is RF12 base station //0xFF is PC uplink

//Setup Network Adapters
HomeNetPortRF12   portRF12(stack);

//Setup attached devices
HomeNetDeviceJeeNode jeeNode(stack);
HomeNetDeviceStatusLights statusLights(stack);
HomeNetDeviceTMP37 tmp37(stack);
HomeNetDeviceLight light(stack);
HomeNetDeviceSwitch lswitch(stack);

//package the setup info in a nice neat arrays
HomeNetPort * ports[] = {&portRF12};
HomeNetDevice * devices[] = {&jeeNode, &statusLights, &tmp37, &light, &lswitch}; 

//delay (sec), frequency (sec), device, sendToNode, sendToDevice, Command, Payload
HomeNetSchedule schedule[] = {{5,10,&tmp37,4,2,CMD_GETVALUE,0},
                              {0,10,&light,4,2,CMD_GETVALUE,0}};
                              
HomeNetInterrupt interrupt[] = {{&lswitch,8,2,CMD_GETVALUE,0}};  

//Setup interrupt
volatile uint8_t deviceInterrupt = 0;
                               
void setup() {
  //initialize HomeNet with the setup info
  stack.init(ports, sizeof(ports)/2, devices, sizeof(devices)/2); 
  stack.registerStatusLights(statusLights); //setup status lights
  stack.registerSchedule(schedule,sizeof(schedule)/sizeof(schedule[0]));
  
  attachInterrupt(1, onInterrupt, CHANGE); //Setup interrupt
}

//Setup interrupt
void onInterrupt(){
  deviceInterrupt++;
}

void loop() {
  //receive incoming packets
  stack.receive();
  stack.process();

  stack.deviceUpdate();
  
  //still working on how often to call these
  stack.receive();
  stack.process();
  
  stack.deviceSchedule();
  
  stack.receive();
  stack.process();
  
  //setup interrupt
  if(deviceInterrupt > 0){
    deviceInterrupt = 0;
    stack.deviceInterrupt();  
  }
}
